package com.servlet;

import com.Service.Impl.ShuihuServiceImpl;
import com.Service.ShuihuService;
import com.Util.IsNotNullUtil;
import com.entity.Shuihu;
import com.qo.PageResult;
import com.qo.ShuiHuQueryObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/shuihu")
public class ShuihuServlet extends HttpServlet {

    private static ShuihuService service = new ShuihuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userReq = req.getParameter("userReq");
        userReq = (userReq != null) ? userReq : "";
        switch (userReq) {
            case "delete":
                delete(req, resp);
                break;
            case "input":
                input(req, resp);
                break;
            case "save":
                save(req, resp);
                break;
            default:
                showAll(req, resp);
        }
    }


    public void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*判断是否登录*/
        //通过会话域获取共享数据
        Object user = req.getSession().getAttribute("USER_IN_SESSION");
        if (user == null) {
            //重定向到登录页面
            req.setAttribute("NotLogin", "请重新登陆");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            //方法结束
            return;
        }
        //分别获取用户从页面传递的分页条件
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //分别获取用户从页面传递的过滤条件
        String keyWord = req.getParameter("keyword");
        String minFighting = req.getParameter("minFighting");
        String maxFighting = req.getParameter("maxFighting");
        //实例化分页过滤条件类对象
        ShuiHuQueryObject shqo = new ShuiHuQueryObject();
        if (IsNotNullUtil.isNotNull(currentPage)) {
            shqo.setCurrentPage(Integer.valueOf(currentPage));
        }
        if (IsNotNullUtil.isNotNull(pageSize)) {
            shqo.setPageSize(Integer.valueOf(pageSize));
        }
        if (IsNotNullUtil.isNotNull(keyWord)){
            shqo.setKeyword(keyWord);
        }
        if (IsNotNullUtil.isNotNull(minFighting)){
            shqo.setMinFighting(Integer.valueOf(minFighting));
        }
        if (IsNotNullUtil.isNotNull(maxFighting)){
            shqo.setMaxFighting(Integer.valueOf(maxFighting));
        }
        //根据封装的分页过滤条件对象,通过Service对象调用方法进行分页页面显示查询结果
        PageResult<Shuihu> pageResult = service.query(shqo);
        //分别将用户传递的分页过滤条件对象和分页页面展示对象共享到作用域中
        req.setAttribute("shqo", shqo);
        req.setAttribute("pageResult", pageResult);
        req.getRequestDispatcher("/WEB-INF/showAll.jsp").forward(req, resp);
        List<Shuihu> ShuiHus = service.selAll();
        req.setAttribute("shuiHus", ShuiHus);
        req.getRequestDispatcher("/WEB-INF/showAll.jsp").forward(req, resp);
    }


    public void input(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        if (IsNotNullUtil.isNotNull(id)) {
            Shuihu shuihu = service.selOneById(Long.valueOf(id));
            req.setAttribute("shuiHu", shuihu);
        }
        req.getRequestDispatcher("/WEB-INF/input.jsp").forward(req, resp);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pnum = req.getParameter("pnum");
        String pname = req.getParameter("pname");
        String fighting = req.getParameter("fighting");
        String riches = req.getParameter("riches");
        String deptNo = req.getParameter("deptNo");
        Shuihu shuihu = new Shuihu();
        if (IsNotNullUtil.isNotNull(pnum)) {
            shuihu.setPnum(pnum);
        }
        if (IsNotNullUtil.isNotNull(pname)) {
            shuihu.setPname(pname);
        }
        if (IsNotNullUtil.isNotNull(fighting)) {
            shuihu.setFighting(Integer.valueOf(fighting));
        }
        if (IsNotNullUtil.isNotNull(riches)) {
            shuihu.setRiches(new BigDecimal(riches));
        }
        if (IsNotNullUtil.isNotNull(deptNo)) {
            shuihu.setDeptNo(Long.valueOf(deptNo));
        }
        String id = req.getParameter("id");
        if (IsNotNullUtil.isNotNull(id)) {
            System.out.println("NULL");
            System.out.println(id);
            shuihu.setId(Long.valueOf(id));
        }
        service.save(shuihu);
        resp.sendRedirect("/shuihu");
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        if (IsNotNullUtil.isNotNull(id)) {
            service.delById(Long.valueOf(id));
        }
        resp.sendRedirect("/shuihu");
    }
}
